package com.snake.mylibrary;

import android.content.Context;
import android.os.Environment;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import com.snake.mylibrary.net.DownloadListener;
import com.snake.mylibrary.net.OkHttpManager;
import com.snake.mylibrary.net.UploadListener;
import com.snake.mylibrary.utils.LogUtil;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;


@RunWith(AndroidJUnit4.class)
public class OkHttpTest {

    String baseUrl = "http://192.168.1.102:8080/httpweb/";

    @Test
    public void okHttpGet(){
        String url = baseUrl+"login?username=get&password=123";
        Context appContext = InstrumentationRegistry.getTargetContext();
        OkHttpManager.init(appContext);
        OkHttpManager.getInstance().httpGet(url);
    }
    @Test
    public void okHttpPost(){
        String url = baseUrl+"login";
        Context appContext = InstrumentationRegistry.getTargetContext();
        OkHttpManager.init(appContext);
        RequestBody requestBody = new FormBody.Builder()
                .add("username","post")
                .add("password","1234")
                .build();
        OkHttpManager.getInstance().httpPost(url, requestBody);
    }
    @Test
    public void okHttpPostString(){
        String url = baseUrl+"postString";
        String param = "{username:snake,password:snake}";
        Context appContext = InstrumentationRegistry.getTargetContext();
        OkHttpManager.init(appContext);
        OkHttpManager.getInstance().httpPostString(url, param);
    }

    @Test
    public void okHttpPostFile(){
        String url = baseUrl+"postFile";
        Context appContext = InstrumentationRegistry.getTargetContext();
        try {
            InputStream is = appContext.getAssets().open("pic1.png");
            String dir = Environment.getExternalStorageDirectory().getAbsolutePath();
            File file = new File(dir,"pic1.jpg");
            FileOutputStream fos = new FileOutputStream(file);
            int len = 0;
            byte[] buf = new byte[1024];

            while((len=is.read(buf))!= -1){
                fos.write(buf, 0, len);
            }

            OkHttpManager.init(appContext);
            OkHttpManager.getInstance().httpPostFile(url, file, new UploadListener() {
                @Override
                public void onRequestProgress(long byteWritten, long contentLength) {
                    LogUtil.e(byteWritten+" / "+contentLength);
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }


    }


    @Test
    public void okHttpPostUploadForm(){
        String url = baseUrl+"uploadForm";
        String dir = Environment.getExternalStorageDirectory().getAbsolutePath();
        File file = new File(dir,"pic1.jpg");
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        MultipartBody body = builder.addFormDataPart("username", "snake")
                .addFormDataPart("password", "321")
                .addFormDataPart("mPhoto", "pic2.jpg", RequestBody.create(MediaType.parse("application/octet-stream"), file))
                .build();
        Context appContext = InstrumentationRegistry.getTargetContext();
        OkHttpManager.init(appContext);
        OkHttpManager.getInstance().httpPostUploadForm(url, body);
    }

    @Test
    public void okHttpDownload(){
        String url = baseUrl+"files/pic1.jpg";
        Context appContext = InstrumentationRegistry.getTargetContext();
        OkHttpManager.init(appContext);
        String dir = Environment.getExternalStorageDirectory().getAbsolutePath();
        File file = new File(dir,"a.jpg");
        OkHttpManager.getInstance().httpGetDownload(url, file, new DownloadListener() {
            @Override
            public void onDownloading(long downloadSize, long total) {
                LogUtil.e(downloadSize+" / "+total);
            }
        });
    }
}
